package com.airforce.controller;


import com.airforce.mappers.awardMapper;
import com.airforce.model.award;
import com.airforce.utils.ExcelUtil;
import com.airforce.utils.ResultCode;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping(value = "index/award/admin")
public class AwardAdmin {



    @Autowired
    awardMapper awardMapper;



   //添加奖品
    @PostMapping(value = "add")
    public Object addTopic(@RequestBody award award){

        if(Optional.ofNullable(award).isPresent()){
            //是否读取Excel文件
            if(Optional.ofNullable(award.getRule()).isPresent()) {
                String path = award.getRule();
                System.out.println("获取文件路径：" + path);
                ExcelUtil.readXlsx(path);
                award.setRule(ExcelUtil.readXlsx(path));

            }
            awardMapper.insertSelective(award);

        }else {
            return ResultCode.NO_PARM.result();
        }
        return ResultCode.SUCCESS.result();
    }


    /**
     * 查询题目
     * @param productname
     * @param createtime
     * @param index
     * @param num
     * @return
     */
    @GetMapping(value = "list")
    public Object awardlist(@RequestParam(required = false) String productname,
                             @RequestParam(required = false) String createtime,
                             @RequestParam(required = false) Integer  index,
                             @RequestParam(required = false) Integer num){

        List<award> list;
        if(productname!=null){

            list=awardMapper.selectByname("%"+productname+"%");
        }else {
            int pageTotal=awardMapper.selectByall().size();
            PageHelper.startPage(index,num);
            list = awardMapper.selectByall();
            return ResultCode.SUCCESS.result(list,pageTotal);
        }
            return ResultCode.SUCCESS.result(list);
    }


    
    @GetMapping(value="find")
    public Object product(@RequestParam Integer id){

        award list;

        if(id!=null) {
            list=awardMapper.selectByPrimaryKey(id);

        }else {
            return ResultCode.Empty_Selct.result();
        }
        return ResultCode.SUCCESS.result(list);
    }

    //删除商品
    @GetMapping(value = "del")
    public Object delProduct(@RequestParam Integer id){

        if(id!=null){
            awardMapper.deleteByPrimaryKey(id);

        }else {
            return ResultCode.NO_PARM.result();
        }
        return ResultCode.SUCCESS.result();
    }


    //更新商品信息

    @PostMapping(value = "edit")
    public Object reditpro(@RequestBody award award){

        if(award!=null){


            awardMapper.updateByPrimaryKeySelective(award);
            return ResultCode.SUCCESS.result();

        }else {
            return ResultCode.NO_PARM.result();
        }
        
    }




}


